package com.cc.bsp.action.common.cmd;

import java.util.List;

import org.springframework.util.Assert;

import com.cc.bsp.action.ICommandAction;
import com.cc.bsp.domain.Domain;
import com.cc.bsp.exception.ActionWebException;
import com.cc.bsp.exception.DomainException;
import com.opensymphony.xwork2.ActionSupport;

public class CommonCMDAction extends ActionSupport implements ICommandAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2826903739372199432L;

	private Domain domain;

	private List<Domain> domains;

	public String create() throws ActionWebException {

		Assert.isNull(domain);

		try {
			domain.create();
		} catch (DomainException e) {
			e.printStackTrace();
			throw new ActionWebException(e);
		}
		return ActionSupport.SUCCESS;
	}

	public String createBatch() throws ActionWebException {

		Assert.notEmpty(domains);

		for (Domain tempDomain : domains) {

			try {
				tempDomain.create();
			} catch (DomainException e) {
				e.printStackTrace();
				throw new ActionWebException(e);
			}
		}

		return ActionSupport.SUCCESS;
	}

	public String update() throws ActionWebException {

		Assert.isNull(domain);

		try {
			domain.update();
		} catch (DomainException e) {
			e.printStackTrace();
			throw new ActionWebException(e);
		}

		return ActionSupport.SUCCESS;
	}

	public String updateBatch() throws ActionWebException {

		Assert.notEmpty(domains);

		for (Domain tempDomain : domains) {

			try {
				tempDomain.update();
			} catch (DomainException e) {
				e.printStackTrace();
				throw new ActionWebException(e);
			}
		}

		return ActionSupport.SUCCESS;
	}

	public String delete() throws ActionWebException {

		Assert.isNull(domain);

		try {
			domain.delete();
		} catch (DomainException e) {
			e.printStackTrace();
			throw new ActionWebException(e);
		}

		return ActionSupport.SUCCESS;
	}

	public String deleteBatch() throws ActionWebException {

		Assert.notEmpty(domains);

		for (Domain tempDomain : domains) {

			try {
				tempDomain.delete();
			} catch (DomainException e) {
				e.printStackTrace();
				throw new ActionWebException(e);
			}
		}

		return ActionSupport.SUCCESS;
	}

	public String execute() throws Exception {

		return super.execute();
	}

	public Domain getDomain() {
		return domain;
	}

	public void setDomain(Domain domain) {
		this.domain = domain;
	}

	public List<Domain> getDomains() {
		return domains;
	}

	public void setDomains(List<Domain> domains) {
		this.domains = domains;
	}

}
